<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
     <!-- <a-button type="primary" icon="download" @click="handleExportXls('bj_recruitment_application')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>-->
       <a-button type="primary" @click="handleWDSQ()">已提交</a-button>
       <a-modal v-model:visible="wdsqVisible" title="我的申请" width="80%">
         <template #footer>
          <a-button key="back" @click="wdsqVisible = false">关闭</a-button>
        </template>
        <applyList ref="applyRef" :type="type"></applyList>
      </a-modal>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a  v-if="record.actStatus == '草稿'" @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
           <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                <a v-if="record.actStatus == '草稿'" >删除</a>
            </a-popconfirm>
           <a-divider type="vertical" />
          <a v-if="record.actStatus == '草稿'" @click="handleTijiao(record)">送审</a>
        </span>

      </a-table>
    </div>

    <bjRecruitmentApplication-modal ref="modalForm" @ok="modalFormOk"></bjRecruitmentApplication-modal>

    <!--提交申请表单-->
        <a-modal title="提交申请" v-model="modalVisible" :mask-closable="false" :width="500" :footer="null">
          <div v-if="modalVisible">
            <a-form-item label="选择审批人" v-show="showAssign">
              <a-select style="width: 100%"
                v-model="form.assignees"
                placeholder="请选择"
                :allowClear="true"
              >
              <a-select-option v-for="(item, i) in assigneeList" :key="i" :value="item.username">{{item.realname}}</a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item label="下一审批人" v-show="isGateway">
              <a-alert  type="info" showIcon message="分支网关处不支持自定义选择下一审批人，将自动下发给所有可审批人。">，将发送给下一节点所有人</a-alert>
            </a-form-item>
            <a-form-item label="优先级" prop="priority">
              <a-select v-model="form.priority" placeholder="请选择" :allowClear="true" style="width: 100%">
                <a-select-option :value="0">普通</a-select-option>
                <a-select-option :value="1">重要</a-select-option>
                <a-select-option :value="2">紧急</a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item label="消息通知">
              <a-checkbox v-model="form.sendMessage">站内消息通知</a-checkbox>
              <a-checkbox v-model="form.sendSms" disabled>短信通知</a-checkbox>
              <a-checkbox v-model="form.sendEmail" disabled>邮件通知</a-checkbox>
            </a-form-item>
            <div slot="footer">
              <a-button type="text" @click="cancelTijiao">取消</a-button>
              <div style="display:inline-block;width: 20px;"></div>
              <a-button type="primary" :disabled="submitLoading" @click="applySubmit">提交</a-button>
            </div>
          </div>
        </a-modal>


  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import applyList from '@/views/activiti/applyList'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import BjRecruitmentApplicationModal from './modules/BjRecruitmentApplicationModal'
  import { postAction } from '../../api/manage'
  import {getAction} from '@/api/manage'
  export default {
    name: "BjRecruitmentApplicationList",
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      BjRecruitmentApplicationModal,applyList
    },
    data () {
      return {
        type: "招工申请",
        wdsqVisible: false,
        description: 'bj_recruitment_application管理页面',
        // 表头
        columns: [
          {
            title:'申请单位',
            align:"center",
            dataIndex: 'sqdw'
          },
          {
            title:'申请岗位',
            align:"center",
            dataIndex: 'sqgw'
          },
          {
            title:'经办人',
            align:"center",
            dataIndex: 'jbren'
          },
          {
            title:'申请时间',
            align:"center",
            dataIndex: 'sqdate'
          },
          {
            title:'岗位工资标准',
            align:"center",
            dataIndex: 'gwgzbz'
          },
          {
            title:'能力素质',
            align:"center",
            dataIndex: 'nlsz'
          },
          {
            title:'招聘原因',
            align:"center",
            dataIndex: 'zpyy'
          },
          {
            title:'用工开始时间',
            align:"center",
            dataIndex: 'ygstartdt'
          },
          {
            title:'用工结束时间',
            align:"center",
            dataIndex: 'ygenddt'
          },
          {
              title:'审批状态',
              align:"center",
              dataIndex: 'actStatus'
           },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            // fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/bj/bjRecruitmentApplication/list",
          delete: "/bj/bjRecruitmentApplication/delete",
          deleteBatch: "/bj/bjRecruitmentApplication/deleteBatch",
          exportXlsUrl: "/bj/bjRecruitmentApplication/exportXls",
          importExcelUrl: "bj/bjRecruitmentApplication/importExcel",
          getFirstNode:'/actProcessIns/getFirstNode',
          applyBusiness:'/actBusiness/apply'
        },
        isGateway: false,
        modalVisible: false,
        showAssign: false,
        assigneeList: [],
        dictOptions:{},
        submitLoading: false,
        error: "",
        form:{
          priority:0,
          assignees:[],
          sendMessage:true
        },
      }
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      handleWDSQ() {
        this.wdsqVisible = true
        this.$nextTick(() => {
          this.$refs.applyRef.loadData()
        })
      },
      handleTijiao(record) {
        postAction('/bj/bjRecruitmentApplication/saveActBussinessDataBeforApprove', { id: record.id }).then((res) => {
             if (res.success) {
                this.apply(res.result);
             } else {
               this.$message.error('失败')
             }
        })
      },
      apply(v) {
              if (!v.procDefId || v.procDefId == "null") {
                this.$message.error("流程定义为空");
                return;
              }
              this.form.id = v.id;
              this.form.procDefId = v.procDefId;
              this.form.title = v.title;
              // 加载审批人
              this.getAction(this.url.getFirstNode,{procDefId:v.procDefId}).then(res => {
                if (res.success) {
                  if (res.result.type == 3 || res.result.type == 4) {
                    this.isGateway = true;
                    this.modalVisible = true;
                    this.form.firstGateway = true;
                    this.showAssign = false;
                    this.error = "";
                    return;
                  }
                  this.form.firstGateway = false;
                  this.isGateway = false;
                  if (res.result.users && res.result.users.length > 0) {
                    this.error = "";
                    this.assigneeList = res.result.users;
                    // 默认勾选
                   // let ids = [];
                   // res.result.users.forEach(e => {
                   //   ids.push(e.username);
                   // });
                    //this.form.assignees = ids;
                    this.form.assignees = [];
                    this.showAssign = true;
                  } else {
                    this.form.assignees = [];
                    this.showAssign = true;
                    this.error = '审批节点未分配候选审批人员，请联系管理员！';
                  }
                  if (this.error){
                    this.$message.error(this.error)
                    return;
                  }
                  this.modalVisible = true;
                }else {
                  this.$message.error(res.message)
                }
              });

            },
            applySubmit() {
                if (this.showAssign && this.form.assignees.length < 1) {
                  this.error = "请至少选择一个审批人";
                  this.$message.error(this.error)
                  return;
                } else {
                  this.error = "";
                }
                this.submitLoading = true;
                var params = Object.assign({},this.form);
               // params.assignees = params.assignees.join(",")
                let that = this;
                this.postFormAction(this.url.applyBusiness,params).then(res => {
                  if (res.success) {
                    this.$message.success("操作成功");
                    that.loadData();
                    this.modalVisible = false;
                  }else {
                    this.$message.error(res.message)
                  }
                }).finally(()=>this.submitLoading = false);
             },
             cancelTijiao(){
               var id=this.form.id;
                postAction('/bj/bjRecruitmentApplication/deleteActBussinessById', { actBussinessid: id }).then((res) => {
                    if (res.success) {
                       this.modalVisible = false;
                    } else {
                      this.$message.error('失败')
                    }
                 })
             },
          initDictConfig(){
          }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>